package com.tianqu.tms.core.util;

import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.security.AccessController;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.taglib.MenuBean;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Author: jzg
 * @Description:
 * @Date: Created by 11:12 on 2018/8/28.
 */
public class MenuTagUtil {

    public static MenuBean getMenuBean(HttpServletRequest request){
        MenuBean rootMenuBean = (MenuBean) request.getSession().getServletContext().getAttribute(TmsConstants.MENU_VO_KEY);
        return rootMenuBean;
    }

    public static void setMenu(HttpServletRequest request, String funcId) {
        MenuBean rootMenuBean = (MenuBean) request.getSession().getServletContext().getAttribute(TmsConstants.MENU_VO_KEY);
        List<MenuBean> topMenuList = MenuTagUtil.getTopMenuList(request, rootMenuBean);
        request.getSession().setAttribute("topMenuList", topMenuList);
        setMenu(request, topMenuList, rootMenuBean, funcId);
    }

    private static void setMenu(HttpServletRequest request, List<MenuBean> topMenuList, MenuBean rootMenuBean, String funcId) {
        if (topMenuList != null && topMenuList.size() > 0) {
            if (StringUtil.isEmpty(funcId)) {
                funcId = topMenuList.get(0).getFunctionId();
            }
            List<MenuBean> rightMenuList = MenuTagUtil.getRightMenuList(request, rootMenuBean, funcId);

            if (rightMenuList != null && rightMenuList.size() > 0) {
                request.getSession().setAttribute("rightMenuList", rightMenuList);
                request.getSession().setAttribute("rightFuncId", rightMenuList.get(0).getFunctionId());
            }
            MenuBean currentMenuBean = rootMenuBean.getMenuVO(funcId);
            if(currentMenuBean.getLevel() == 0){
                request.getSession().setAttribute("topFuncId", funcId);
            }else if (currentMenuBean.getLevel() == 1) {
                request.getSession().setAttribute("topFuncId", funcId);
            }else{
                request.getSession().setAttribute("rightFuncId", funcId);
            }
        }
    }


    private static List<MenuBean> getTopMenuList(HttpServletRequest request, MenuBean rootMenuBean) {
        List<MenuBean> list = new ArrayList<MenuBean>();
        buildNavigation(request, list, rootMenuBean, 0, "");
        return list;
    }

    private static List<MenuBean> getRightMenuList(HttpServletRequest request, MenuBean rootMenuBean, String funcId) {
        List<MenuBean> list = new ArrayList<MenuBean>();
        MenuBean currentMenuBean = rootMenuBean.getMenuVO(funcId);
        buildNavigation(request, list, currentMenuBean, 1, currentMenuBean.getFunctionId());
        return list;
    }

    /**
     * 判定用户是否有权限
     *
     * @param MenuBean
     * @return
     */
    private static boolean hasViewCompetence(MenuBean MenuBean, HttpServletRequest request) {
        if (MenuBean.isNeedsRole()) {
            String funcId = MenuBean.getFunctionId();
            UserContext uc =
                    (UserContext) request.getSession().getAttribute(
                            TmsAction.APPLICATION_USER_CONTEXT_KEY);
            TmsUserProfile userProfile = uc.getUserProfile();
            try {
                AccessController accessController = AccessController.getInstance();
                return accessController.checkPermission(userProfile, funcId);
            } catch (Exception e) {
                return false;
            }
        }
        return true;
    }

    /**
     * 格式如下：
     *
     * @param menuBean
     */
    private static void buildNavigation(HttpServletRequest request, List<MenuBean> list, MenuBean menuBean, int level, String funcId) {
        int menuBeanLevel = menuBean.getLevel();
        switch (menuBeanLevel) {
            case 0:
                //==============================================================
                //     第0层
                //==============================================================
                break;
            case 1:
                //==============================================================
                //     第一层
                //==============================================================
                // 判断是否有权限
                if (hasViewCompetence(menuBean, request) && !funcId.equals(menuBean.getFunctionId())) {
                    list.add(menuBean);
                }
                break;
            case 2:
                //==============================================================
                //     第二层
                //==============================================================
                // 判断是否有权限
                if (hasViewCompetence(menuBean, request) && !funcId.equals(menuBean.getFunctionId())) {
                    list.add(menuBean);
                }
                break;
            default:
                break;
        }

        //======================================================================
        //     其子菜单加载
        //======================================================================
        if (hasViewCompetence(menuBean, request)) {
            if (menuBeanLevel == level) {
                List children = menuBean.getChildrenList();

                if (children != null && children.size() > 0) {
                    Iterator childrenIt = children.iterator();
                    while (childrenIt.hasNext()) {
                        MenuBean targetBean = (MenuBean) childrenIt.next();
                        buildNavigation(request, list, targetBean, level, funcId);
                    }
                }
            }
        }

    }

}
